<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\OrderLease;
use App\Models\ShopSms;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Services\OrderService;
use Illuminate\Console\Command;

class OverdueReminderBeyondCommand extends BaseCommand
{
    const LIMIT_NUM = 1;//一个月发送短信次数

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'overdueReminderBeyond';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单逾期超出15天短信提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    protected function handleExec()
    {
        try {
            $where = [
                ['ol.begin_time','<=',strtotime(date('Y-m-d 23:59:59',strtotime('-1 day')))],
            ];
            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id','o.status as order_status','ol.id as lease_id','m.cert_name','m.mobile','o.seller_id','o.trade_no',
                    'o.begin_time as order_begin_time','o.end_time as order_end_time','ol.begin_time','ol.status as lease_status')
                ->leftjoin('sp_order_lease_2 as ol', 'o.id','ol.order_id')
                ->leftjoin('sp_member_2 as m', 'o.uid','m.id')
                ->where('o.status',OrderService::ORDER_STATUS_OVERDUE)
                ->where('ol.begin_time','<',strtotime('-7 day'))
                ->where('ol.status',OrderLease::ORDER_LEASE_STATUS_OVERDUE)
                ->where($where)
                ->groupBy('ol.order_id')
                ->orderBy('ol.id', 'asc')
                ->get();
            $data = obToArray($data);
            $start_date = date('Y-m-01 00:00:00', strtotime(date('Y-m')));
            $end_date = date('Y-m-d 23:59:59', strtotime("$start_date +1 month -1 day"));
            if($data){
             foreach ($data as $k=>$item){
               $overDay = floor((strtotime(date('Y-m-d H:i:s')) - $item['begin_time']) / 86400);
                 if($overDay>15){
                    $where1 = [];
                    $where1[] = ['create_time','>=',$start_date];
                    $where1[] = ['create_time','<=',$end_date];
                    $where1[] = ['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE_BEYOND];
                    $where1[] = ['order_id',$item['trade_no']];
                    $count = SmsRecord::where($where1)->count('id');
                    if($count >= self::LIMIT_NUM){
                        continue;
                    }else{
    //                    $overdue_amount = OrderLease::where(['order_id'=>$item['id'],'status'=>OrderLease::ORDER_LEASE_STATUS_OVERDUE])->sum('rent');
    //                    if($overdue_amount > 0){
                            //发送短信
                            $res = ShopSms::overdueReminderBeyond($item['seller_id'],$item['mobile'],$item['trade_no'],$item['cert_name'],$item['order_begin_time'],$item['order_end_time']);
    //                    }
                    }
                }
             }
          }
            $this->infoLog('订单逾期短信提醒执行成功',$data);
        }catch (\Exception $exception){
            $this->infoLog('订单逾期短信提醒执行失败'.$exception->getMessage());
        }
    }
}
